#version 330 core

in vec3 Normal;
in vec2 TexCoords;
in vec3 FragPos;

struct Material
{
	sampler2D diffuse1;
	vec3 diffuse2;
	sampler2D specular1;
	vec3 specular2;
	float shininess;
};

uniform Material material;

struct DirLight
{
	vec3 direction;
	vec3 ambient;
	vec3 diffuse;
	vec3 specular;
};

uniform DirLight dirLight;

uniform vec3 viewPos;

out vec4 FragColor;

vec3 CalcDirLight(DirLight light, vec3 normal, vec3 viewDir)
{
	vec3 lightDir = normalize(-light.direction);
	float diff = max(dot(normal, lightDir), 0.0);
	
    vec3 reflectDir = reflect(-lightDir, normal);
	float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);

	vec3 ambient = light.ambient * (material.diffuse2 + texture(material.diffuse1, TexCoords).rgb);
	vec3 diffuse = light.diffuse * (material.diffuse2 + texture(material.diffuse1, TexCoords).rgb) * diff;
	vec3 specular = light.specular * (material.specular2 + texture(material.specular1, TexCoords).rgb) * spec;
	return ambient + diffuse + specular;
}

void main()
{
    vec3 norm = normalize(Normal);
    vec3 viewDir = normalize(viewPos - FragPos);
    vec3 result = CalcDirLight(dirLight, norm, viewDir);
    FragColor = vec4(result, 1.0);
}